import { Inject, Injectable, UploadedFiles } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import * as Oss from 'ali-oss';

@Injectable()
export class UploadService {
  @Inject('ALIOSS_CLIENT')
  private client: Oss;

  @Inject(ConfigService)
  private configService: ConfigService;

  async getOssPolicy() {
    // 定义签名的有效期：为 1 天
    const expireDate = new Date(Date.now() + 24 * 60 * 60 * 1000);

    // 创建临时凭证
    const policy = this.client.calculatePostSignature({
      expiration: expireDate.toISOString(),
      conditions: [
        ['content-length-range', 0, 1048576000], // 设置上传文件的大小限制: 0B ~ 1GB
      ],
    });

    // 获取存储桶信息
    const bucketName = this.configService.get('alioss.bucket_name');
    const bucketLocation = this.configService.get('alioss.region');
    // 获取完整的 host
    const host = `https://${bucketName}.${bucketLocation}.aliyuncs.com`;

    return {
      ...policy,
      host,
    };
  }
}
